package com.blq.developtools

import android.content.Intent
import android.view.View
import blq.ssnb.baseconfigure.simple.MenuBean
import blq.ssnb.baseconfigure.simple.SimpleMenuActivity
import blq.ssnb.mvp.MvpCallBack
import blq.ssnb.snbutil.SnbToast
import com.blq.developtools.activity.LayoutViewerActivity
import com.blq.developtools.service.DevelopToolsService
import com.blq.developtools.utils.DevelopUtils
import com.blq.developtools.utils.JumpUtils


class MainActivity : SimpleMenuActivity() {

    override fun navigationTitle(): String {
        return ""
    }

    override fun getMenuBeans(): MutableList<MenuBean> {
        val list: MutableList<MenuBean> = mutableListOf()
        list.add(MenuBean().apply {
            menuTitle = "判断是否有悬浮权限";
            onClickListener = View.OnClickListener {
                permissionCheck(arrayOf(Permission.OverFlow), object : MvpCallBack<Boolean> {
                    override fun onSuccess(backData: Boolean?) {

                    }

                    override fun onFail(errorCode: Int, msg: String?) {

                    }
                })
            }
        })

        list.add(MenuBean().apply {
            menuTitle = "布局边界";
            menuSubTitle = "先判断是否有无障碍权限"
            onClickListener = View.OnClickListener {
                permissionCheck(arrayOf(Permission.ACCESSIBILITY), object : MvpCallBack<Boolean> {
                    override fun onSuccess(backData: Boolean?) {
                        DevelopToolsService.showLayoutInspector(context)
                    }

                    override fun onFail(errorCode: Int, msg: String?) {
                        SnbToast.showShort(msg = "失败")
                    }

                })
            }
        })
        list.add(MenuBean().apply {
            menuTitle = "悬浮窗";
            menuSubTitle = "MessageInfo"
            onClickListener = View.OnClickListener {
                permissionCheck(arrayOf(Permission.OverFlow), object : MvpCallBack<Boolean> {
                    override fun onSuccess(backData: Boolean?) {
                        SnbToast.showShort(msg = "成功")
                        DevelopToolsService.showLayoutInspector(context)
                    }

                    override fun onFail(errorCode: Int, msg: String?) {
                        SnbToast.showShort(msg = "失败")
                    }

                })
            }
        })

        list.add(MenuBean().apply {
            menuTitle = "点击弹框";
            menuSubTitle = "MessageInfo"
            onClickListener = View.OnClickListener {
                permissionCheck(arrayOf(Permission.OverFlow), object : MvpCallBack<Boolean> {
                    override fun onSuccess(backData: Boolean?) {
                        SnbToast.showShort(msg = "成功1")
                    }

                    override fun onFail(errorCode: Int, msg: String?) {
                        SnbToast.showShort(msg = "失败")
                    }

                })
            }
        })

        list.add(MenuBean().apply {
            menuTitle = "点击弹框";
            menuSubTitle = "MessageInfo"
            onClickListener = View.OnClickListener {
                permissionCheck(arrayOf(Permission.OverFlow), object : MvpCallBack<Boolean> {
                    override fun onSuccess(backData: Boolean?) {
                        SnbToast.showShort(msg = "成功2")
                        val intent = Intent(context, LayoutViewerActivity::class.java)
//                        DevelopUtils.findRootParent(it.parent)
                        intent.putExtra("is",true)
                        startActivity(intent)
                    }

                    override fun onFail(errorCode: Int, msg: String?) {
                        SnbToast.showShort(msg = "失败")
                    }

                })
            }
        })

        return list
    }


    fun permissionCheck(permissions: Array<Permission>, callBack: MvpCallBack<Boolean>) {
        var isPass = true
        for (permission in permissions) {
            when (permission) {
                Permission.OverFlow -> {
                    if (!DevelopUtils.isFloatWindowAllowed(context)) {
                        isPass = false
                        JumpUtils.jump2FloatingSetting(context, packageName)
                    }

                }

                Permission.ACCESSIBILITY -> {
                    if (!DevelopUtils.isAccessibilitySettingsOn(
                            context,
                            DevelopToolsService::class.java
                        )
                    ) {
                        isPass = false;
                        JumpUtils.jump2Accessibility(context);
                    }
                }
            }
            if (!isPass) {
                break
            }
        }

        if (isPass) {
            callBack.onSuccess(true)
        } else {
            callBack.onFail(-1, "")
        }
    }


    enum class Permission {
        OverFlow,
        ACCESSIBILITY
    }
}